﻿using DotNetOpenAuth.AspNet.Clients;
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;
using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration;
using DotNetOpenAuth.OpenId.RelyingParty;
using Inovout.Runtime;
using System.Collections.Generic;

namespace Inovout.Web.Security
{
    public class InovoutOpenIdClient: OpenIdClient
    {
        public static readonly string OpenIdProviderName = "InovoutOpenId";
        public InovoutOpenIdClient()
            : base(OpenIdProviderName, ComponentRegistry.Resolve<IApplication>().Settings["OpenIdProviderEndpoint"].ToString())
        { }
        public static readonly string MewmbershipNameKey = "membership.name";
        protected override Dictionary<string, string> GetExtraData(IAuthenticationResponse response)
        {
            //FetchResponse extension = response.GetExtension<FetchResponse>();
            //if (extension != null)
            //{
            //    Dictionary<string, string> dictionary = new Dictionary<string, string>();
            //    dictionary.Add(MewmbershipNameKey, extension.GetAttributeValue(WellKnownAttributes.Name.FullName));
            //    return dictionary;
            //}
            var claimsResponse = response.GetExtension<ClaimsResponse>();
            if (claimsResponse != null)
            {
                Dictionary<string, string> dictionary = new Dictionary<string, string>();
                dictionary.Add(MewmbershipNameKey, claimsResponse.FullName);
                return dictionary;
            }   

            return null;
        }
        protected override void OnBeforeSendingAuthenticationRequest(IAuthenticationRequest request)
        {
            //FetchRequest fetchRequest = new FetchRequest();
            //AXUtilities.AddRequired(fetchRequest.Attributes, WellKnownAttributes.Name.FullName);
            //request.AddExtension(fetchRequest);

            request.AddExtension(new ClaimsRequest
            {
                FullName = DemandLevel.Require,
            });
        }
    }
}